﻿using System;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin;
using Microsoft.Owin.Security.Cookies;
using Owin;
using Sharp.Portal.Owin;

namespace Sharp.WebBooter
{
	public partial class Startup
	{
		public void ConfigureAuth(IAppBuilder app)
		{
		    app.CreatePerOwinContext<SharpUserManager>(SharpUserManager.Create);
		    app.CreatePerOwinContext<SharpSignInManager>(SharpSignInManager.Create);
		    // 使应用程序可以使用 Cookie 来存储已登录用户的信息
		    app.UseCookieAuthentication(new CookieAuthenticationOptions
		    {
		        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
		        LoginPath = new PathString("/Account/Login"),
                Provider = new CookieAuthenticationProvider
                {
                    OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<SharpUserManager, SharpUser, int>(
                        TimeSpan.FromDays(7),
                        (manager, user) => user.GenerateUserIdentityAsync(manager),
                        id => int.Parse(id.GetUserId()))
                },
                //CookieHttpOnly = true,
                CookieName = "__SharpTicketSid",
		        //SlidingExpiration = true,
		        ExpireTimeSpan = TimeSpan.FromDays(7)
		    });

		    app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

		    app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(30));

		    app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
        }
	}
}